<?php
/**
 * 24tao首页展示
 * author liwei
 * date 2013-06-23
 */
class RecommonAction extends CommonAction {


	function index(){

		$Star = M('star');
		$starInfo = $Star->where('area=1')->select();
		//在保存时清空SESSION否则删除
		if($_SESSION['photoname']){
			foreach($_SESSION['photoname'] as $key=>$val){
				@unlink("./Public/Uploads/Photo/".session(C('USER_AUTH_KEY')).'/'.$val);
			}
			$_SESSION['photoname'] = array();
		} 
		$this->assign('starinfo', $starInfo);
		$this->assign('search', $search);
		$this->display();
	}

	function getAjaxStar(){
		$stararea = $_GET['stararea'];

		$Star = M('star');
		$starInfo = $Star->where("area=$stararea")->select();
		$html = '<select name="star_id" id="star_id">';
		foreach( $starInfo as $key=>$val ){
			$html .= '<option value="'.$val['id'].'">'.$val['chinese_name'].'</option>';
		}
		$html .= '<option value="orther" onchange="getStarStatus(this.value)">其他</option></select>';

		echo $html;
	}

	 /**
     * 预览保存上传图片
     */
    function uploadpic() {
        import("@.ORG.UploadFile");
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 2048000;
        //设置上传文件类型
        $upload->allowExts = explode(',', 'jpg,gif,png,jpeg');
        //设置附件上传目录
        $upload->savePath = $_SERVER['DOCUMENT_ROOT'] . __ROOT__ . '/Public/Uploads/Photo/'.session(C('USER_AUTH_KEY')).'/';
        //设置需要生成缩略图，仅对图像文件有效
        $upload->thumb = true;
        // 设置引用图片类库包路径
        $upload->imageClassPath = '@.ORG.Image';
        //设置需要生成缩略图的文件后缀
        $upload->thumbPrefix = 'm_'; //产生1张缩略图
        //设置缩略图最大宽度
        $upload->thumbMaxWidth = '220';
        //设置缩略图最大高度
        $upload->thumbMaxHeight = '280';
        //设置上传文件规则
        $upload->saveRule = uniqid;
        //删除原图
        $upload->thumbRemoveOrigin = true;
        if ($upload->upload()) {
            $uploadList = $upload->getUploadFileInfo();
        }
		$i = count($_SESSION['photoname']);
		$_SESSION['photoname'][$i+1] = session(C('USER_AUTH_KEY')).'/m_' . $uploadList[0]['savename'];

        echo session(C('USER_AUTH_KEY')).'/m_' . $uploadList[0]['savename'];
    }

	function addRecom(){
		$data['uid'] = session(C('USER_AUTH_KEY'));
		$data['star_id'] = $_POST['star_id'];
		$data['source_url'] = $_POST['img_source'];
		$data['recom_title'] = $_POST['title'];
		$data['price'] = $_POST['price'];
		$data['recom_reason'] = $_POST['reason'];
		$data['createtime'] = date('Y-m-d H:i:s');
		if( $_SESSION['photoname'] ){
			$i = 1;
			foreach($_SESSION['photoname'] as $key=>$val){
				$data['recom_pic'.$i] = $val;
				$i++;
			}
		}
		
		$RecomGood = M('recom_good');
		if( !empty($data['star_id']) ){
			 if ( $RecomGood->create() ) {
                if ( false !== $RecomGood->add($data) ) {
					$_SESSION['photoname'] = array();
                    $this->assign("jumpUrl","index");
                    $this->success('数据添加成功！');
                } else {
                    $this->assign("jumpUrl","index");
                    $this->error('数据写入错误');
                }
            }
		}
	}

	function delPhoto(){
		$id = $_GET['id'];
		@unlink("./Public/Uploads/Photo/".$_SESSION['photoname'][$id]);
		unset($_SESSION['photoname'][$id]);
		$this->ajaxReturn(true);
	}
}